package com.mars.tukuan.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mars.base.common.base.BaseController;
import com.mars.base.common.base.BaseResult;
import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.Paginator;
import com.mars.tukuan.dto.CarUserVo;
import com.mars.tukuan.entity.CarUser;
import com.mars.tukuan.rpc.api.CarUserService;

@RestController
@RequestMapping("/api/tukuan/mobile/car/user")
public class CarUserController extends BaseController {

	@Autowired
	private CarUserService service;

	@PostMapping(value = "/add")
	public Object add(@RequestBody CarUser entity) {
		System.out.println(entity.getCity());
		if(StringUtils.isBlank(entity.getUsername())) {
			return BaseResult.build(false, "用户名不能为空");
		}
		if(StringUtils.isBlank(entity.getEmail())) {
			return BaseResult.build(false, "邮箱地址不能为空");
		}
		if(StringUtils.isBlank(entity.getMobile())) {
			return BaseResult.build(false, "手机号码不能为空");
		}
		if (null != this.service.checkValue("username", entity.getUsername())) {
			return BaseResult.build(false, String.format("该登录账号【%s】已存在", entity.getUsername()));
		}
		if (null != this.service.checkValue("email", entity.getEmail())) {
			return BaseResult.build(false, String.format("该邮箱地址【%s】已存在", entity.getEmail()));
		}
		if (null != this.service.checkValue("mobile", entity.getMobile())) {
			return BaseResult.build(false, String.format("该手机号码【%s】已存在", entity.getMobile()));
		}
		try {
			this.service.insertSelective(entity);
		} catch (Exception e) {
			return BaseResult.addFailed();
		}
		return BaseResult.addOk();
	}

	@GetMapping(value = "/delete/{ids}")
	public Object delete(@PathVariable String ids) {
		try {
			this.service.deleteByIds(ids);
		} catch (Exception e) {
			return BaseResult.deleteFailed();
		}
		return BaseResult.deleteOk();
	}

	@PostMapping(value = "/update")
	public Object update(@RequestBody CarUser entity) {
		try {
			CarUser oldUser = this.service.selectById(entity.getId()) ;
			if(null != oldUser.getUsername() && !oldUser.getUsername().equals(entity.getUsername())) {
				if(null != this.service.checkValue("username", entity.getUsername())) {
					return BaseResult.build(false, String.format("该登录账号【%s】已存在", entity.getUsername()));
				}
			}
			if(null != oldUser.getEmail() && !oldUser.getEmail().equals(entity.getEmail())) {
				if(null != this.service.checkValue("email", entity.getEmail())) {
					return BaseResult.build(false, String.format("该邮箱地址【%s】已存在", entity.getEmail()));
				}
			}
			if(null != oldUser.getMobile() && !oldUser.getMobile().equals(entity.getMobile())) {
				if(null != this.service.checkValue("mobile", entity.getMobile())) {
					return BaseResult.build(false, String.format("该手机号码【%s】已存在", entity.getMobile()));
				}
			}
			this.service.updateSelective(entity);
		} catch (Exception e) {
			return BaseResult.updateFailed();
		}
		return BaseResult.updateOk();
	}

	@GetMapping(value = "/get/{id}")
	public CarUser get(@PathVariable Long id) {
		return this.service.selectById(id);
	}

	@GetMapping(value = "/list")
	public Object list(CarUserVo userVo) {
		try {
			return  this.service.list(new Paginator(request), userVo) ;
		} catch (AppException e) {
			return BaseResult.loadFailed();
		}
	}
	
	/**
	 * 终端用户登陆验证
	 * @param mobile
	 * @param password
	 * @return
	 */
	@PostMapping(value = "/login")
	public Object login(String mobile, String password) {
		try {
			return this.service.validateLogin(mobile, password) ;
		} catch (Exception e) {
			return BaseResult.failure();
		}
	}
	
	/**
	 * 终端用户修改密码
	 * @param mobile
	 * @param password
	 * @return
	 */
	@PostMapping(value = "/updatePassword")
	public Object updatePassword(String mobile, String oldPassword, String newPassword) {
		try {
			return this.service.updatePassword(mobile, oldPassword, newPassword) ;
		} catch (Exception e) {
			return BaseResult.failure();
		}
	}
	
	/**
	 * 锁定或解锁帐号
	 * @param ids
	 * @return
	 */
	@GetMapping(value = "/updateAccountStatus")
	public Object updateAccountStatus(Long[] ids, Integer status) {
		System.out.println(ids);
		try {
			Integer updateAccountStatus = this.service.updateAccountStatus(ids, status) ;
			if(updateAccountStatus > 0) {
				if(status == 1) {
					return BaseResult.build(true, "解锁成功") ;
				} else {
					return BaseResult.build(true, "锁定成功") ;
				}
			} else {
				return BaseResult.failure() ;
			}
		} catch (AppException e) {
			return BaseResult.failure() ;
		}
	}
	
	/**
	 * 重置密码
	 * @param ids
	 * @return
	 */
	@GetMapping(value = "/resetPassword")
	public Object resetPassword(Long[] ids) {
		try {
			return this.service.resetPassword(ids) ;
			
		} catch (AppException e) {
			return BaseResult.failure() ;
		}
	}

}
